﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;

namespace UserGroupData
{
    public class SponsorPaymentRepository : ISponsorPaymentRepository
    {
        public SponsorPaymentDTO GetSponsorPayment(int id)
        {
            using (var ctx = new UserGroupEntities())
            {
                var meeting = ctx.SponsorPayments.Where(d => d.Id == id);
                var entity = meeting.FirstOrDefault();
                Mapper.CreateMap<SponsorPayment, SponsorPaymentDTO>();
                var dto = Mapper.Map<SponsorPayment, SponsorPaymentDTO>(entity);
                return dto;
            }
        }

        public List<SponsorPaymentDTO> GetAllSponsorPayments()
        {
            using (var ctx = new UserGroupEntities())
            {
                var entities = ctx.SponsorPayments.ToList();
                Mapper.CreateMap<SponsorPayment, SponsorPaymentDTO>();
                var dto = Mapper.Map<List<SponsorPayment>, List<SponsorPaymentDTO>>(entities);
                return dto;
            }
        }

        public int AddNewSponsorPayment(SponsorPaymentDTO dto)
        {
            using (var ctx = new UserGroupEntities())
            {
                Mapper.CreateMap<SponsorPaymentDTO, SponsorPayment>();
                var entity = Mapper.Map<SponsorPaymentDTO, SponsorPayment>(dto);
                ctx.AddToSponsorPayments(entity);
                ctx.SaveChanges();
                return entity.Id;
            }
        }

        public void UpdateSponsorPayment(SponsorPaymentDTO dto)
        {
            using (var ctx = new UserGroupEntities())
            {
                Mapper.CreateMap<SponsorPaymentDTO, SponsorPayment>();
                var entity = Mapper.Map<SponsorPaymentDTO, SponsorPayment>(dto);
                ctx.ApplyCurrentValues("SponsorPayments", entity);
                ctx.SaveChanges();
            }
        }

        public void DeleteSponsorPaymentDTO(int id)
        {
            using (var ctx = new UserGroupEntities())
            {
                SponsorPayment dbDept = ctx.SponsorPayments.First(d => d.Id == id);
                ctx.DeleteObject(dbDept);
                ctx.SaveChanges();
            }
        }
    }
}
